package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.data.DataBase;
import com.entity.User;
import com.google.gson.Gson;

@WebServlet(urlPatterns={"/loginSuc"})
public class LoginSucServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		Map<String, User> data = DataBase.data;
		
		String[] keys=new String[data.size()];
		
		data.keySet().toArray(keys);
		
		for (String string : keys) {
			System.out.println(data.get(string));
		}
		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		
		PrintWriter out = resp.getWriter();
		
		Gson gson=new Gson();
		
		//获得用户
		User user=(User)req.getSession().getAttribute("user");
		User user2=new User(user.getUsername(), "", user.getAddress());
		out.print(gson.toJson(user2));
		
	}
}
